package com.chongchong.common.tool;

import com.chongchong.common.enums.ApiErrorCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.lang.Nullable;
import org.springframework.web.client.ResourceAccessException;
import org.springframework.web.client.RestClientException;
import org.springframework.web.client.RestTemplate;

import java.util.Map;

/**
 * @author 作者: Elvira
 * @date 创建时间: 2020年12月19日 下午4:49:37
 *
 *       http连接类
 */
@Slf4j
public class BaseHttpClient {

	private static final RestTemplate restTemplate = new RestTemplate();

	public static <T> T post(String url, @Nullable Object request, Class<T> responseType) {
		T body = null;
		try {
			log.info("调用接口路径:url={}", url);
			ResponseEntity<T> response = restTemplate.postForEntity(url, request, responseType);
			if (response != null) {
				body = response.getBody();
				log.info("调用接口返回数据为:response={}", response);
			}
			return body;
		} catch (ResourceAccessException e) {
			log.error("调用接口连接失败");
			log.error(e.getMessage(), e);
			throw new ApiNetworkException(ApiErrorCode.NETWORK_ERROR);
		} catch (RestClientException e) {
			log.error("调用接口网络异常,调用服务失败");
			log.error(e.getMessage(), e);
			throw new ApiNetworkException(ApiErrorCode.NETWORK_ERROR);
		} catch (Exception e) {
			log.error("调用接口异常");
			log.error(e.getMessage(), e);
			throw e;
		}
	}

	public static <T> T get(String url, Class<T> responseType) {
		T response = null;
		try {
			response = restTemplate.getForObject(url, responseType);
			log.info("调用接口返回数据为:response={}", response);
			return response;
		} catch (ResourceAccessException e) {
			log.error("调用接口连接失败");
			log.error(e.getMessage(), e);
			throw new ApiNetworkException(ApiErrorCode.NETWORK_ERROR);

		} catch (RestClientException e) {
			log.error("调用接口网络异常,调用服务失败");
			log.error(e.getMessage(), e);
			throw e;
		} catch (Exception e) {
			log.error("调用接口异常");
			log.error(e.getMessage(), e);
			throw e;
		}
	}

	public static <T> T get(String url, @Nullable Object request, Class<T> responseType) {
		T body = null;
		try {
			ResponseEntity<T> response = restTemplate.getForEntity(url, responseType);
			if (response != null) {
				body = response.getBody();
				log.info("调用接口返回数据为:response={}", response);
			}
			return body;
		} catch (ResourceAccessException e) {

			log.error("调用接口连接失败");
			log.error(e.getMessage(), e);
			throw new ApiNetworkException(ApiErrorCode.NETWORK_ERROR);

		} catch (RestClientException e) {
			log.error("调用接口网络异常,调用服务失败");
			log.error(e.getMessage(), e);
			throw e;

		} catch (Exception e) {
			log.error("调用接口异常");
			log.error(e.getMessage(), e);
			throw e;
		}
	}

	public static <T> T getForEntity(String url,Map<String, ?> map, Class<T> responseType) {
		T body = null;
		try {
			ResponseEntity<T> response = restTemplate.getForEntity(url, responseType,map);
			if (response != null) {
				body = response.getBody();
				log.info("调用接口返回数据为:response={}", response);
			}
			return body;
		} catch (ResourceAccessException e) {

			log.error("调用接口连接失败");
			log.error(e.getMessage(), e);
			throw new ApiNetworkException(ApiErrorCode.NETWORK_ERROR);

		} catch (RestClientException e) {
			log.error("调用接口网络异常,调用服务失败");
			log.error(e.getMessage(), e);
			throw e;

		} catch (Exception e) {
			log.error("调用接口异常");
			log.error(e.getMessage(), e);
			throw e;
		}
	}
}
